package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ClippingMediaSource;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.LoopingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSourceEventListener;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.RawResourceDataSource;
import com.google.android.exoplayer2.video.VideoListener;
import com.photoeditor.R;
import com.photoeditor.demo.db.SubtitleAnimBean2;
import com.photoeditor.demo.model.event.AddVideoTextEveryVideoEvent;
import com.photoeditor.demo.model.event.AgeSuccessEvent;
import com.photoeditor.demo.model.event.FeedBackEvent;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.filter.VideoSubtitleFilter;
import com.photoeditor.demo.videofilter.com.daasuu.epf.EPlayerView;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import androidx.annotation.Nullable;

import static com.photoeditor.demo.ui.activity.subscribe.SubscribeVideoActivity.getVideoResIds;
import static com.photoeditor.demo.ui.activity.subscribe.SubscribeVideoActivity.getVideoStartAndEndPosMsList;

/**
 * Created by cjh on 2020/2/6.
 * 这个类是关于视频播放暂停等操作的类
 */
public class SubscribePlayerView extends EPlayerView {

    //SubtitleAnimBean2里面是关于视频里面文字平移、缩放等变换的一些bean
    private List<SubtitleAnimBean2> mAnimBeans;
    private SimpleExoPlayer player;
    protected VideoSubtitleFilter subtitleFilter;
    protected VideoSubtitleFilter subtitleFilter1;
    protected VideoSubtitleFilter subtitleFilter2;
    protected VideoSubtitleFilter subtitleFilter3;
    protected VideoSubtitleFilter subtitleFilter4;
    protected VideoSubtitleFilter subtitleFilter5;
    protected VideoSubtitleFilter subtitleFilter6;
    private int videoResId;
    private long startPositionUs;
    private long endPositionUs;
    private long mStartPosMs;
    private long mEndPosMs;
    private boolean mVideoPaused;
    private boolean mFirstStart = true;
    private boolean mDelayStartCalled;
    private View mVideoForegroundView;
    private long mPausedPosMs = -1L;

    private int mOnFailedImgResId;
    private int mOnFailedTextResId;

    //存放多个视频的源文件id
    private int[] videoResIds;
    private boolean isFunctionSubscribe;
    private ArrayList<long[]> videoStartAndEndPosMsList;
    private int videoIndex=0;

    private ControlHandler mHandler;
    public SubscribePlayerView(Context context) {
        super(context);
    }

    public void setOnFailedImgResId(int resId) {
        this.mOnFailedImgResId = resId;
    }

    public void setOnFailedTextResId(int resId) {
        this.mOnFailedTextResId = resId;
    }

    public SubscribePlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void sendMessage(int what) {
        sendMessageDelayed(what, 0L);
    }

    private void sendMessageDelayed(int what, long delay) {
        if (mHandler == null) {
            return;
        }
        mHandler.removeMessages(what);
        mHandler.sendEmptyMessageDelayed(what, delay);
    }

    @Override
    public void onResume() {
        super.onResume();
        //1.发消息通知准备播放
        sendMessageDelayed(ControlHandler.MSG_WHAT_START, ControlHandler.DELAY_START);
        if (player != null) {
            player.setVolume(0f);
        }
    }

    //2.开始播放
    public void startVideo() {
        if (player == null) {
            return;
        }

        if (mPausedPosMs >= 0) {
            player.seekTo(mPausedPosMs);
            mPausedPosMs = -1L;
            onVideoResume();
        } else {
            player.seekTo(mStartPosMs);
            //3.播放并开始绘制字幕
            onVideoStart();
        }
        if (!player.getPlayWhenReady()) {
            player.setPlayWhenReady(true);
        }
    }

    public void onVideoStart() {
        Log.e("player", "onVideoStart");
        mVideoPaused = false;

        if (subtitleFilter != null) {
            return;
        }


        sendMessageDelayed(ControlHandler.MSG_WHAT_HIDE_COVER, ControlHandler.DELAY_HIDE_COVER);
        //通过不断发消息去判断当前进度是属于哪一个视频
        if(!isFunctionSubscribe){//多个视频合成一个视频的方法(非功能订阅页)
            sendMessageDelayed(ControlHandler.MSG_WHAT_CHECK, ControlHandler.INTERVAL_CHECK_MS);
            return;
        }
        //功能订阅页往下走
        subtitleFilter = new VideoSubtitleFilter(player);
        subtitleFilter.setBeans(mAnimBeans);
        subtitleFilter.startDraw();
        setGlFilter(subtitleFilter);

    }


    @Override
    public void onPause() {
        super.onPause();

        if (player != null) {
            mPausedPosMs = player.getCurrentPosition();
            if (player.getPlayWhenReady()) {
                player.setPlayWhenReady(false);
            }
        }

        onVideoPause();
    }

    @Override
    public void onDestroy() {
        destroyVideo();
        super.onDestroy();
    }

    private void destroyVideo() {
        mHandler.removeMessages(ControlHandler.MSG_WHAT_START);
        mHandler.removeMessages(ControlHandler.MSG_WHAT_CHECK);
        mHandler.removeMessages(ControlHandler.MSG_WHAT_HIDE_COVER);

        onVideoStop();

        if (player != null) {
            player.stop();
            player.release();
            player = null;
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

    }

    public void setup(long startPosMs, long endPosMs,boolean isFunctionSubscribe,String entrance) {
        this.mStartPosMs = startPosMs;
        this.mEndPosMs = endPosMs;
        mHandler = new ControlHandler(this);
        //真正设置SimpleExoPlayer
        setupSimpleExoPlayer(isFunctionSubscribe,entrance);
        setSimpleExoPlayer(player);
    }

    public void setVideoForegroundView(View view) {
        mVideoForegroundView = view;
    }

    private void setupSimpleExoPlayer(boolean isFunctionSubscribe,String entrance) {
        this.isFunctionSubscribe=isFunctionSubscribe;
        LoopingMediaSource loopingMediaSource=null;
        //得到要播放视频的int[]集合
        videoResIds=getVideoResIds();
        //得到视频Id+开始时间和结束时间
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
        try {
            if(isFunctionSubscribe){//是功能订阅页，获取对应的视频
                //功能订阅页对应的视频
                switch (entrance) {
                    case "aging":
                        videoResId=R.raw.vip_video_aging;
                        startPositionUs=360000;//微秒
                        endPositionUs=8160000;
                        break;
                    case "gender":
                        videoResId=R.raw.vip_video_gender;
                        startPositionUs=360000;//微秒
                        endPositionUs=7680000;
                        break;
                    case "past":
                        videoResId=R.raw.vip_video_past;
                        startPositionUs=360000;//微秒
                        endPositionUs=4400000;
                        break;
                    case "baby":
                        videoResId=R.raw.vip_video_baby;
                        startPositionUs=360000;//微秒
                        endPositionUs=5800000;
                        break;
                    default:
                        break;
                }
                //只播放一个视频
                DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResId));
                RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(getContext().getApplicationContext());
                rawResourceDataSource.open(dataSpec);
                DataSource.Factory factory = () -> rawResourceDataSource;
                ExtractorMediaSource mediaSource= new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);

                // Clip to start at 5 seconds and end at 10 seconds.
                ClippingMediaSource clippingSource =
                        new ClippingMediaSource(
                                mediaSource,
                                /* startPositionUs= */ startPositionUs,
                                /* endPositionUs= */ endPositionUs);

                loopingMediaSource = new LoopingMediaSource(clippingSource);
            }else{
                //第一个视频
                DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResIds[0]));
                RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(getContext().getApplicationContext());
                rawResourceDataSource.open(dataSpec);
                DataSource.Factory factory = () -> rawResourceDataSource;
                ExtractorMediaSource mediaSource1 = new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);


                //第二个视频
                dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResIds[1]));
                rawResourceDataSource.open(dataSpec);
                ExtractorMediaSource mediaSource2 = new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);

                //第三个视频
                dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResIds[2]));
                rawResourceDataSource.open(dataSpec);
                ExtractorMediaSource mediaSource3 = new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);

                //第四个视频
                dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResIds[3]));
                rawResourceDataSource.open(dataSpec);
                ExtractorMediaSource mediaSource4 = new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);

                //第五个视频
                dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResIds[4]));
                rawResourceDataSource.open(dataSpec);
                ExtractorMediaSource mediaSource5 = new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);

                //第六个视频
                dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(videoResIds[5]));
                rawResourceDataSource.open(dataSpec);
                ExtractorMediaSource mediaSource6 = new ExtractorMediaSource(rawResourceDataSource.getUri(),
                        factory, new DefaultExtractorsFactory(), null, null);

                MediaSource source = new ConcatenatingMediaSource(mediaSource1, mediaSource2, mediaSource3, mediaSource4, mediaSource5, mediaSource6);

                //使这两个视频轮流循环播放
                loopingMediaSource = new LoopingMediaSource(source);

            }

            // SimpleExoPlayer
            player = ExoPlayerFactory.newSimpleInstance(getContext().getApplicationContext(), trackSelector);
            player.addListener(new Player.EventListener() {
                @Override
                public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {

                }

                @Override
                public void onPlayerError(ExoPlaybackException error) {

                }

                @Override
                public void onSeekProcessed() {

                }

                @Override
                public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                    Log.e("player", "==============> state: " + playbackState);
                    /*
                    *   Player.STATE_IDLE：这是初始状态，播放器停止时的状态以及播放失败时的状态。
                        Player.STATE_BUFFERING： player 无法立即从当前位置进行播放。这主要是因为需要加载更多数据。
                        Player.STATE_READY： player 可以立即从当前位置进行播放。
                        Player.STATE_ENDED：播放器播放完所有媒体。
                        除了这些状态之外， player 还有一个 playWhenReady标志来指示用户打算播放。
                        如果状态为 Player.STATE_READY和 playWhenReady=true，则播放器仅播放。
                    *
                    *
                    * */

                    if (playWhenReady && playbackState == Player.STATE_READY) {
                        // Active playback.
                        Log.e("player", "playWhenReady && playbackState == Player.STATE_READY");

                    } else if (playWhenReady) {
                        // Not playing because playback ended, the player is buffering, stopped or
                        // failed. Check playbackState and player.getPlaybackError for details.
                        if (player.getPlaybackError() != null) {
                            Log.e("player", "play failed!!!!");
                            mHandler.sendEmptyMessage(ControlHandler.MSG_WHAT_VIDEO_FAILED);
                        }
                        Log.e("player", "playWhenReady");
                        mVideoPaused = false;

                    } else if(playbackState == Player.STATE_ENDED) {
                        Log.e("player", "END");
                    }
                }
            });
            player.addVideoListener(new VideoListener() {
                @Override
                public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {

                }

                @Override
                public void onSurfaceSizeChanged(int width, int height) {

                }

                @Override
                public void onRenderedFirstFrame() {
                    Log.e("player", "onRenderedFirstFrame");
                }
            });
            // Prepare the player with the source.
            //1.循环播放这几个视频

            player.prepare(loopingMediaSource);

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public EPlayerView setSimpleExoPlayer(SimpleExoPlayer player) {
        super.setSimpleExoPlayer(player);
        return this;
    }

    public void onVideoResume() {
        Log.e("player", "onVideoResume");

        if (subtitleFilter != null) {
            subtitleFilter.startDraw();
        }
    }

    public void onVideoPause() {
        Log.e("player", "onVideoPause");

        if (subtitleFilter != null) {
            subtitleFilter.stopDraw();
        }
    }

    public void onVideoStop() {
        Log.e("player", "onVideoStop");

//        mHandler.removeMessages(ControlHandler.MSG_WHAT_CHECK);

        if (subtitleFilter != null) {
            subtitleFilter.stopDraw();
        }

        removeFilter();
        subtitleFilter = null;
    }

    public void onCheckVideoPosition() {
    }

    public long getCurrentPositionMs() {
        if (player == null) {
            return -1;
        }

        return player.getCurrentPosition();
    }
    public long getContentDuration() {
        if (player == null) {
            return -1;
        }

        return player.getContentDuration();
    }

    public long getContentPosition () {
        if (player == null) {
            return -1;
        }

        return player.getContentPosition();
    }


    public long getDuration() {
        if (player == null) {
            return -1;
        }

        return player.getDuration();
    }

    public long getStartPosMs() {
        return mStartPosMs;
    }

    public long getEndPosMs() {
        return mEndPosMs;
    }

    public void seekToStart() {
        if (player == null) {
            return;
        }

        player.seekTo(mStartPosMs);
    }

    public void hideCover() {
        if (mVideoForegroundView != null) {
            mVideoForegroundView.setVisibility(GONE);
        }
    }

    public void onVideoPlayFailed() {
        // 无法播放视频，改为显示图片
        destroyVideo();
        if (mVideoForegroundView instanceof ImageView) {
            if (mOnFailedImgResId > 0) {
                ((ImageView) mVideoForegroundView).setImageResource(mOnFailedImgResId);
            }
        } else if (mVideoForegroundView instanceof VideoForegroundView) {
            VideoForegroundView view = (VideoForegroundView) mVideoForegroundView;
            view.setImageResId(mOnFailedImgResId);
            view.setText(mOnFailedTextResId);
        }
        mVideoForegroundView.setVisibility(VISIBLE);

        setVisibility(INVISIBLE);
    }

    private class ControlHandler extends Handler {

        public static final int MSG_WHAT_CHECK = 1000;
        public static final int MSG_WHAT_HIDE_COVER = 2000;
        public static final int MSG_WHAT_START = 3000;
        public static final int MSG_WHAT_VIDEO_FAILED = 4000;

        public static final long INTERVAL_CHECK_MS = 450L;
        public static final long DELAY_HIDE_COVER = 100L;
        public static final long DELAY_START = 500L;

        public  ArrayList<Long> everyVideoDuration;
        public int videoIndex;

        private WeakReference<SubscribePlayerView> mRef;

        public ControlHandler(SubscribePlayerView view) {
            this.mRef = new WeakReference<>(view);
        }

        @Override
        public void handleMessage(Message msg) {
            SubscribePlayerView subscribePlayerView = mRef.get();
            if (subscribePlayerView == null) {
                return;
            }

            switch (msg.what) {
                case MSG_WHAT_START:
                    subscribePlayerView.startVideo();
                    break;
                case MSG_WHAT_CHECK:
                    sendEmptyMessageDelayed(ControlHandler.MSG_WHAT_CHECK, ControlHandler.INTERVAL_CHECK_MS);
                    long currentDuration=subscribePlayerView.getDuration();
                    if(currentDuration<=0){
                        return;
                    }
                    everyVideoDuration=getVideoStartAndEndPosMsList();
                    if(currentDuration==everyVideoDuration.get(0)){//第一段视频
                        /*
                        因为现在是由好多段小视频组成的一个完整视频。现在无法获得整个视频的进度和总时长。
                        拿到的只是每段视频的总时长，下一个视频进度又是从0开始，因此，这里通过写死每段视频
                        的总时长，来判断是哪一个视频，从而发消息绘制相对应视频的字幕
                        *
                        * */
                        //表示假设同一个视频进来两次，不需要绘制，直接退出
                        if(subtitleFilter6!=null){
                            return;
                        }
                        //清空存字幕的集合
                        if(mAnimBeans!=null){
                            mAnimBeans.clear();
                            mAnimBeans=null;
                        }
                        //停止上一次的绘制
                        if(subtitleFilter5!=null){
                            subtitleFilter5.stopDraw();
                            removeFilter();
                            subtitleFilter5 = null;
                        }
                        //发消息添加字幕(滤镜两行，发两次)
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.sub_video_beauty),240L,true,false));
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.sub_video_cartoon_effect),240L,true,true));

                        if (mAnimBeans == null) {
                            return;
                        }
                        //开始绘制
                        subtitleFilter6 = new VideoSubtitleFilter(player);
                        subtitleFilter6.setBeans(mAnimBeans);
                        subtitleFilter6.startDraw();
                        setGlFilter(subtitleFilter6);

                    }else if(currentDuration==everyVideoDuration.get(1)){//第二段
                        //开始初始化开始绘制,停止上一次的绘制
                        if(subtitleFilter1!=null){
                            return;
                        }
                        if(mAnimBeans!=null){
                            mAnimBeans.clear();
                            mAnimBeans=null;
                        }
                        if(subtitleFilter6!=null){
                            subtitleFilter6.stopDraw();
                            removeFilter();
                            subtitleFilter6 = null;
                        }
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.sub_video_facing_aging),600L,false,false));
                        if (mAnimBeans == null) {
                            return;
                        }
                        subtitleFilter1 = new VideoSubtitleFilter(player);
                        subtitleFilter1.setBeans(mAnimBeans);
                        subtitleFilter1.startDraw();
                        setGlFilter(subtitleFilter1);
                    }else if(currentDuration==everyVideoDuration.get(2)){//第三段
                        //开始初始化开始绘制
                        if(subtitleFilter2!=null){
                            return;
                        }
                        if(mAnimBeans!=null){
                            mAnimBeans.clear();
                            mAnimBeans=null;
                        }
                        if(subtitleFilter1!=null){
                            subtitleFilter1.stopDraw();
                            removeFilter();
                            subtitleFilter1 = null;
                        }
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.sub_video_gender_swap),600L,false,false));
                        if (mAnimBeans == null) {
                            return;
                        }
                        subtitleFilter2 = new VideoSubtitleFilter(player);
                        subtitleFilter2.setBeans(mAnimBeans);
                        subtitleFilter2.startDraw();
                        setGlFilter(subtitleFilter2);
                    }else if(currentDuration==everyVideoDuration.get(3)){//第四段
                        //开始初始化开始绘制
                        if(subtitleFilter3!=null){
                            return;
                        }
                        if(mAnimBeans!=null){
                            mAnimBeans.clear();
                            mAnimBeans=null;
                        }
                        if(subtitleFilter2!=null){
                            subtitleFilter2.stopDraw();
                            removeFilter();
                            subtitleFilter2 = null;
                        }
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.sub_video_past_life),600L,false,false));
                        if (mAnimBeans == null) {
                            return;
                        }
                        subtitleFilter3 = new VideoSubtitleFilter(player);
                        subtitleFilter3.setBeans(mAnimBeans);
                        subtitleFilter3.startDraw();
                        setGlFilter(subtitleFilter3);
                    }else if(currentDuration==everyVideoDuration.get(4)){//第五段--宝宝预测
                        //开始初始化开始绘制
                        if(subtitleFilter4!=null){
                            return;
                        }
                        if(mAnimBeans!=null){
                            mAnimBeans.clear();
                            mAnimBeans=null;
                        }
                        if(subtitleFilter3!=null){
                            subtitleFilter3.stopDraw();
                            removeFilter();
                            subtitleFilter3 = null;
                        }
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.baby_prediction),600L,false,false));
                        if (mAnimBeans == null) {
                            return;
                        }
                        subtitleFilter4 = new VideoSubtitleFilter(player);
                        subtitleFilter4.setBeans(mAnimBeans);
                        subtitleFilter4.startDraw();
                        setGlFilter(subtitleFilter4);
                    }else {//第六段--动物贴纸
                        //开始初始化开始绘制
                        if(subtitleFilter5!=null){
                            return;
                        }
                        if(mAnimBeans!=null){
                            mAnimBeans.clear();
                            mAnimBeans=null;
                        }
                        if(subtitleFilter4!=null){
                            subtitleFilter4.stopDraw();
                            removeFilter();
                            subtitleFilter4 = null;
                        }
                        EventBus.getDefault().post(new AddVideoTextEveryVideoEvent(getContext().getString(R.string.sub_video_animal_stickers),600L,false,false));
                        if (mAnimBeans == null) {
                            return;
                        }
                        subtitleFilter5 = new VideoSubtitleFilter(player);
                        subtitleFilter5.setBeans(mAnimBeans);
                        subtitleFilter5.startDraw();
                        setGlFilter(subtitleFilter5);
                    }
                    break;
                case MSG_WHAT_HIDE_COVER:
                    subscribePlayerView.hideCover();
                    break;
                case MSG_WHAT_VIDEO_FAILED:
                    subscribePlayerView.onVideoPlayFailed();
                    break;
            }
        }
    }

    @Override
    public void onRenderedFirstFrame() {
        super.onRenderedFirstFrame();
    }

    /**
     * 固定位置的文字
     *
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMills
     * @param endPositionMills
     * @param x
     * @param y
     */
    public void addSubtitle(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMills, long endPositionMills, int x, int y) {
        if (endPositionMills - startPositionMills <= 0) {
            return;
        }

        SubtitleAnimBean2 bean = new SubtitleAnimBean2(text, textSize, textColor, textFont, startPositionMills, endPositionMills, x, y);
        if (mAnimBeans == null) {
            mAnimBeans = new LinkedList<>();
        }
        mAnimBeans.add(bean);
    }

    /**
     * 透明度变化效果文字
     *
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMills
     * @param endPositionMills
     * @param x
     * @param y
     * @param fromAlpha
     * @param toAlpha
     */
    public void addSubtitleAlphaAnim(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMills, long endPositionMills, int x, int y, float fromAlpha, float toAlpha) {
        if (endPositionMills - startPositionMills <= 0) {
            return;
        }

        SubtitleAnimBean2 bean = new SubtitleAnimBean2(text, textSize, textColor, textFont, startPositionMills, endPositionMills, x, y, fromAlpha, toAlpha);
        if (mAnimBeans == null) {
            mAnimBeans = new LinkedList<>();
        }
        mAnimBeans.add(bean);
    }

    /**
     * 位移和透明度和缩放变化文字
     *
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMills 动画开始时间
     * @param endPositionMills   动画结束时间
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     * @param fromAlpha
     * @param toAlpha
     */
    public void addSubtitleTranslateScaleAlphaAnim(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMills, long endPositionMills, int fromX, int toX, int fromY, int toY, float fromAlpha, float toAlpha, float fromScale, float toScale) {
        if (endPositionMills - startPositionMills <= 0) {
            return;
        }

        SubtitleAnimBean2 bean = new SubtitleAnimBean2(text, textSize, textColor, textFont, startPositionMills, endPositionMills, fromX, toX, fromY, toY, fromAlpha, toAlpha,fromScale, toScale);
        if (mAnimBeans == null) {
            mAnimBeans = new LinkedList<>();
        }
        mAnimBeans.add(bean);
    }

    /**
     * 位移和缩放变化文字
     *
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMills 动画开始时间
     * @param endPositionMills   动画结束时间
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     *
     */
    public void addSubtitleTranslateScaleAnim(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMills, long endPositionMills, int fromX, int toX, int fromY, int toY,float fromScale, float toScale) {
        if (endPositionMills - startPositionMills <= 0) {
            return;
        }

        SubtitleAnimBean2 bean = new SubtitleAnimBean2(text, textSize, textColor, textFont, startPositionMills, endPositionMills, fromX, toX, fromY, toY,fromScale, toScale,0f);
        if (mAnimBeans == null) {
            mAnimBeans = new LinkedList<>();
        }
        mAnimBeans.add(bean);
    }


    /**
     * 位移和透明度变化文字
     *
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMills 动画开始时间
     * @param endPositionMills   动画结束时间
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     * @param fromAlpha
     * @param toAlpha
     */
    public void addSubtitleTranslateAlphaAnim(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMills, long endPositionMills, int fromX, int toX, int fromY, int toY, float fromAlpha, float toAlpha) {
        if (endPositionMills - startPositionMills <= 0) {
            return;
        }

        SubtitleAnimBean2 bean = new SubtitleAnimBean2(text, textSize, textColor, textFont, startPositionMills, endPositionMills, fromX, toX, fromY, toY, fromAlpha, toAlpha);
        if (mAnimBeans == null) {
            mAnimBeans = new LinkedList<>();
        }
        mAnimBeans.add(bean);
    }
}
